<?php
namespace jy_merchteamfee;
defined('BY_JYA') or exit('error');
/*
	分销插件工具类
*/
class model extends \plugin{
	
	public function checklogin(){
		cfc('core')->checklogin();
		$this->model('member')->register();
	}
	public function current_level_up_types(){
		return str_array($this->setting['level_up_types']);
	}
	public function current_daili_file_types(){
		return str_array($this->setting['daili_file']);
	}
	public function available_areas(){
		$areas=file_get_contents(ROOT_D.'/core/resource/area_town.txt');
		$areas=unserialize($areas);
		return $areas;
	}
	public function get_area_code($area){
		$address=$area['province'];
		if($area['city']){
			$address.=' '.$area['city'];
		}
		if($area['district']){
			$address.=' '.$area['district'];
		}
		if($area['street']){
			$address.=' '.$area['street'];
		}
		$area_code=pdo_getcolumn('core_area',array('address'=>$address),'code');
		if(!$area_code){
			$isup=pdo_count('core_area',array(),'code');
			if($isup==0){
				require ROOT_D.'/update/update_area.php';
			}
		}
		return $area_code;
	}
	public function checkmaxfee($order){
		$this->order=$order;
		$this->commission_order=$order;
		$level=$this->get('level',array('id'=>$agent['level_id'],'uniacid'=>$_SESSION['uniacid']));
		$iscommission=$this->get('commission',array('type'=>2,'ordersn'=>$this->order['ordersn'],'uniacid'=>$_SESSION['uniacid']));
		if($iscommission['id']){
			exi('该订单已经领取了最大平级奖励，不能在领取了！','error');
		}
		if($myteam['id']){
			$fromuser=$myteam['fromuser'];
		}else{
			$fromuser=$this->getcolumn('member',array('mid'=>$order['mid'],'uniacid'=>$_SESSION['uniacid'],'merchid'=>$_SESSION['onemerchid']),'fromuser');
		}
		if($myteam['level_id']){
			$this->nowlevel=$this->getcolumn('level',array('id'=>$myteam['level_id']),'level');
		}else{
			$this->nowlevel=0;
		}
		$parameter=array(
			'params'=>array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$_SESSION['onemerchid']),
			'orderby'=>'level asc',
			'limit'=>'1',
		);		
		$levels=$this->get_list('level',$parameter);
		$minlevel=$levels[0]['level'];
		$parameter['orderby']='level desc';
		$levels=$this->get_list('level',$parameter);
		$this->commission_order['maxlevel']=$maxlevel=$levels[0]['level'];
		$levelnum=12;	
		//极差分红单线等级ABCDE，第一级没有分红，低于自己的没有分红，平级没有分红
		for($i=0;$i<$levelnum;$i++){
				if(!$fromuser){
					break;
				}
				$team=$this->get('team',array('mid'=>$fromuser,'uniacid'=>$_SESSION['uniacid'],'merchid'=>$_SESSION['onemerchid']));
				$fromuser=$this->getcolumn('member',array('mid'=>$fromuser,'uniacid'=>$_SESSION['uniacid'],'merchid'=>$_SESSION['onemerchid']),'fromuser');
				$level=$this->getcolumn('level',array('id'=>$team['level_id']),'level');
				$level=$level?:0;
				//上等级不是最大级，当前等级小于上等级或小于最小等级，都没有分红
				if($this->nowlevel!=$maxlevel && ($level<=$minlevel || $level<$this->nowlevel)){
					continue;
				}
				if($this->ismaxping && $level==$maxlevel){
					continue;
				}
				if($level==$this->nowlevel){
					$isping=1;
				}
				//上次是最大级，并且当前等级也是最大
				if($this->nowlevel==$maxlevel && $level==$maxlevel){
					$this->create_redcommission($team,$level,$isping);
					$this->ismaxping=1;
				}
				//当等级匹配时，分红
				$this->nowlevel=$level;
				//如果他的上级是我
				$ta_agent_id=$this->getcolumn('member',array('mid'=>$fromuser,'merchid'=>$_SESSION['onemerchid']),'fromuser');
				if($ta_agent_id==$team['mid']){
					break;
				}
		}
	}
	function create_redcommission($agent,$key_level='',$isping=0){
			$commission_level=$agent['level_id'];
			if(!$agent['level_id']){
				//获取默认等级分红比例
				return false;
			}else{
				$level=$this->get('level',array('id'=>$agent['level_id'],'uniacid'=>$_SESSION['uniacid']));
				$level['setdata']=json_decode($level['setdata'],true);
				$level_id=$level['id'];
				//开启最大平级分红？
				if($this->setting['open_maxteamfee'] && $key_level==$this->commission_order['maxlevel'] && $this->nowlevel==$key_level){
					$commission_per=$this->setting['maxteamfee_rate'];
					$ismaxred=true;
				}else{
					return false;
				}
			}
			$money=0;
			$order_price=$this->commission_order['commission_price'];
			$remark=array(
				'level_id'=>$level_id,
				'order_price'=>$order_price,
				'goods_commission'=>array(),
			);			
			//先检查是否存在商品独立分红规则，如果存在则对订单相应商品部分金额单独计算分红
			$commission_level='red'.$level_id;
			foreach($this->order['goods'] as $goods){
				$ignore=$this->get('goods',array('goods_id'=>$goods['goodsid'],'uniacid'=>$_SESSION['uniacid']));
				if($ignore['ignore']  || !$ignore['id']){
					$goods_commission=array();
					if($ignore['ignore']){
						$order_price=$order_price-$total_price;
					}
					$this->needorder=true;
					continue;
				}else{
					$goods_commission=$ignore['redfee']?json_decode($ignore['redfee'],true):array();
				}
				$order_price=$order_price-$goods['total_price'];
				//开启最大平级分红？
				if($ignore['open_maxteamfee'] && $ismaxred){
					$goods_commission_per=$ignore['maxteamfee_rate'];
				}else{
					$goods_commission_per=$goods_commission[$commission_level];
				}
				if(substr($goods_commission_per,-1)=='%'){
					$goods_commission_money=intval($goods['total_price']*substr($goods_commission_per,0,-1))/100;
					if(!$ismaxred){
						$goods_commission_money -=$this->commission_order['red_money'];
					}
					$money+=$goods_commission_money;
				}else{
					$goods_commission_money=intval($goods_commission_per*$goods['total']*100)/100;
					if(!$ismaxred){
						$goods_commission_money -=$this->commission_order['red_money'];
					}
					$money+=$goods_commission_money;
				}
				$remark['goods_commission'][]=array('goods_id'=>$goods['goodsid'],'total_price'=>$goods['total_price'],'total'=>$goods['total'],'commission_per'=>$goods_commission_per,'commission_money'=>$goods_commission_money);
			}
			if($order_price>0 && ($this->needorder|| $ismaxred)){
				$order_commission_money=intval($order_price*$commission_per)/100;
				$money+=$order_commission_money;
				$remark['order_commission']=array('order_price'=>$order_price,'commission_per'=>$commission_per,'commission_money'=>$order_commission_money);
			}
			$remark['commission_money']=$money;
			if($money>0 && $ismaxred){
				$this->commission_order['isgetmaxred']=1;
				$this->commission_order['commission_money']+=$money;
				$new_commission=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$agent['mid'],
					'agent_id'=>$agent['id'],
					'area_code'=>$agent['area_code'],
					'level'=>$level_id,
					'ordersn'=>$this->commission_order['ordersn'],
					'money'=>$money,
					'remark'=>ijson_encode($remark),
					'createtime'=>TIMESTAMP,
					'status'=>$this->setting['is_jiesuan']?:0,
					'type'=>$ismaxred?2:1,
					'merchid'=>$_SESSION['onemerchid']
				);
				$this->insert('commission',$new_commission);
			}
	}
}